package com.nova.bluetooth.ui.bluetoothDemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.nova.bluetooth.R;
import com.nova.bluetooth.adapter.ConfiguredAdapter;
import com.nova.bluetooth.base.BaseActivity;
import com.nova.bluetooth.bean.DeviceConfigBean;
import com.nova.bluetooth.databinding.ActivityMainBinding;
import com.nova.bluetooth.helpUtils.ToastUtils;
import com.nova.bluetooth.manage.MMKVManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class MainActivity extends BaseActivity {

    private ConfiguredAdapter mAdapter;
    private ActivityMainBinding binding;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
        initData();
        setPaddingBottom(this, binding.getRoot());


        String inputDateString = "2025-03-15"; // 初始化你的输入日期字符串
        boolean isBefore = isDateBeforeCurrentDate(inputDateString);
        if (isBefore) { //过期了
            ToastUtils.show("请联系开发者获取新版本");
            finish();
        } else {
            ToastUtils.show("为您跳转到功能调试Demo...");
        }

        ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        // 处理返回的结果
                        Intent data = result.getData();
                        String returnedData = data.getStringExtra("key");
                        Log.d("MainActivity", "返回的数据：" + returnedData);

                        //刷新列表
                        initData();
                    }
                });
        findViewById(R.id.btn_add_config).setOnClickListener(v -> {
            // 启动目标 Activity
            Intent intent = new Intent(this, ConfigFunctionActivity.class);
            activityResultLauncher.launch(intent);
        });


        //test
//        LogUtil.d("getVersionFromData : " +
//                BluetoothDataUtil.getVersionFromData(new byte[]{0x01, 0x02, 0x04, 0x60, 0x05}));


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initView() {
        mAdapter = new ConfiguredAdapter();
        RecyclerView recyclerView = findViewById(R.id.configDeviceRecy);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.iv_delete) {
                    mAdapter.remove(mAdapter.getData().get(position));

                    if (mAdapter.getData().isEmpty()) {
                        binding.llEmptyView.setVisibility(View.VISIBLE);
                        binding.configDeviceRecy.setVisibility(View.GONE);
                    } else {
                        binding.llEmptyView.setVisibility(View.GONE);
                        binding.configDeviceRecy.setVisibility(View.VISIBLE);
                    }

                    updateConfigData();
                } else if (view.getId() == R.id.content) {
                    Intent intent = new Intent(MainActivity.this, ScanBLEActivity.class);
                    intent.putExtra(ScanBLEActivity.DEVICE_CONFIG, mAdapter.getData().get(position));
                    startActivity(intent);
                }
            }
        });
    }

    /*
     * 更新配置数据
     */
    private void updateConfigData() {
        List<DeviceConfigBean> mData = mAdapter.getData();
        MMKVManager.saveDeviceConfigBeanList(mData);
    }


    private void initData() {
        List<DeviceConfigBean> mData = new ArrayList<>();
        // 读取用户列表
        List<DeviceConfigBean> loadedUserList = MMKVManager.getDeviceConfigBeanList();
        if (loadedUserList != null && !loadedUserList.isEmpty()) {
            for (DeviceConfigBean data : loadedUserList) {
                mData.add(data);
                Log.d("MainActivity",
                        "getDeviceName: " + data.getDeviceName() +
                                ", getDeviceType: " + data.getDeviceType() +
                                ", getSupportList: " + data.getSupportList());
            }
            mAdapter.setList(mData);

            binding.llEmptyView.setVisibility(View.GONE);
            binding.configDeviceRecy.setVisibility(View.VISIBLE);
        } else {
            Log.d("MainActivity", "No user list found");

            binding.llEmptyView.setVisibility(View.VISIBLE);
            binding.configDeviceRecy.setVisibility(View.GONE);
        }
    }
}